<?php
namespace app\mini\model;

use think\facade\Db;
use app\common\traits\ActionRecordTrait;
use think\Model;
use app\admin\controller\ApiCommon;

use app\crm\model\CustomerLossModel;
use app\crm\model\OrderStatistics;
use app\admin\model\ShopOrderModel;
use app\admin\model\CustomerModel;
use app\admin\model\ShopModel;
use app\shop\model\Refund as OrderModel;
use app\mini\model\MiniStisticsModel;

class RefundAmount extends Model

{

    /**

     * @var string

     */

    protected $name = 'shop_refund_amount';



    protected $defaultSoftDelete = 0;



    protected $autoWriteTimestamp = true;

    /**

     * 获取系统首页信息资源

     * @return array

     * @author ran

     * @date 2021-09-23 13:53

     * mailbox 466180170@qq.com

     */
    public function get_refund_amount($startTime,$endTime,$shopIds)
    {
        $where = [];

        array_push($where,['create_time','>=',$startTime]);
        array_push($where,['create_time','<=',$endTime]);
        array_push($where,['shop_id','in',$shopIds]);

        $refund = Db::name('shop_refund_amount')->where($where)->select()->toArray();

        $new_single = array_values(array_unique(array_column($refund,'new_single'))); // 新单

        $repairOrder = array_values(array_unique(array_column($refund,'repairOrder'))); // 补单

        $scar_amount = array_values(array_unique(array_column($refund,'scar_amount'))); // 疤痕

        $project_amount = array_values(array_unique(array_column($refund,'project_amount'))); // 项目

        $amount_money = array_values(array_unique(array_column($refund,'amount_money'))); // 实收

        $referral_amount = array_values(array_unique(array_column($refund,'referral_amount'))); // 转诊

        $total_amount = array_values(array_unique(array_column($refund,'total_amount'))); // 总业绩



        $sum_new_single = bcadd(array_sum($new_single),0,2);

        $sum_repairOrder = bcadd(array_sum($repairOrder),0,2);

        $sum_scar_amount = bcadd(array_sum($scar_amount),0,2);

        $sum_project_amount = bcadd(array_sum($project_amount),0,2);

        $sum_amount_money = bcadd(array_sum($amount_money),0,2);

        $sum_referral_amount = bcadd(array_sum($referral_amount),0,2);

        $sum_total_amount = bcadd(array_sum($total_amount),0,2);


        return [

            'new_single' => $sum_new_single,

            'repairOrder' => $sum_repairOrder,

            'scar_amount' => $sum_scar_amount,

            'project_amount' => $sum_project_amount,

            'amount_money' => $sum_amount_money,

            'referral_amount' => $sum_referral_amount,

            'total_amount' => $sum_total_amount,

        ];

    }
}